package com.ruoyi.forum.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.forum.constant.TopicReplyTypeEnum;
import com.ruoyi.forum.dao.CircleTopicReplyDao;
import com.ruoyi.forum.domain.CircleNoticeRead;
import com.ruoyi.forum.domain.CircleTopicReply;
import com.ruoyi.forum.model.TopicGreantStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * service
 *
 * @author lxk
 * @date 2018-10-17 10:05:39
 */
@Service("circleTopicReplyService")
public class CircleTopicReplyService {

    @Autowired
    private CircleTopicReplyDao circleTopicReplyDao;
    @Autowired
    private CircleNoticeReadService circleNoticeReadService;


    public List<Long> getGreatTopicId(String uid) {
        // 构造查询条件: uid等于指定值 且 replyType等于TopicReplyTypeEnum.GREAT.value() 且 delFlag等于0
        LambdaQueryWrapper<CircleTopicReply> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CircleTopicReply::getTopicId)
                .eq(CircleTopicReply::getUid, uid)
                .eq(CircleTopicReply::getReplyType, TopicReplyTypeEnum.GREAT.value())
                .eq(CircleTopicReply::getDelFlag, 0);

        // 执行查询并返回topicId列表
        List<CircleTopicReply> replies = circleTopicReplyDao.list(wrapper);
        return replies.stream()
                .map(CircleTopicReply::getTopicId)
                .collect(Collectors.toList());
    }


    public Integer getGreatStatus(String uid, Long topicId) {
        // 构造查询条件: uid等于指定值 且 replyType等于TopicReplyTypeEnum.GREAT.value() 且 delFlag等于0 且 topicId等于指定值
        LambdaQueryWrapper<CircleTopicReply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CircleTopicReply::getUid, uid)
                .eq(CircleTopicReply::getReplyType, TopicReplyTypeEnum.GREAT.value())
                .eq(CircleTopicReply::getDelFlag, 0)
                .eq(CircleTopicReply::getTopicId, topicId);

        // 执行计数查询
        long count = circleTopicReplyDao.count(wrapper);
        if (count > 0) {
            return 1;
        }
        return 0;
    }


    public Map<Long, Long> getGreatStatus(String uid, List<Long> topicId) {
        List<TopicGreantStatus> topicGreantStatusList = circleTopicReplyDao.getTopicIdsGreatStatus(uid, topicId);

        if (!CollectionUtils.isEmpty(topicGreantStatusList)) {
            Map<Long, Long> topicGreantStatusMap = topicGreantStatusList.stream().collect(
                    Collectors.toMap(TopicGreantStatus::getTopicId, TopicGreantStatus::getCount, (k1, k2) -> k1));
            return topicGreantStatusMap;
        } else {
            return null;
        }
    }


    /**
     * 修改已读状态
     *
     * @param uid
     * @param circleNoticeRead
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateReadStatus(String uid, CircleNoticeRead circleNoticeRead) {
        // 使用 LambdaUpdateWrapper 构造更新条件
        LambdaUpdateWrapper<CircleTopicReply> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(CircleTopicReply::getReadFlag, 1)
                .eq(CircleTopicReply::getReadFlag, 0)
                .ne(CircleTopicReply::getUid, uid)
                .and(wrapper -> wrapper
                        .inSql(CircleTopicReply::getTopicId, "SELECT id FROM circle_topic WHERE uid = '" + uid + "'")
                        .or()
                        .eq(CircleTopicReply::getReplyTo, uid));

        // 执行更新操作
        circleTopicReplyDao.update(null, updateWrapper);

        circleNoticeReadService.update(circleNoticeRead);

    }


    /**
     * 修改已读状态
     *
     * @param uid
     * @param topicIds
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateReadFlag(String uid, List<Long> topicIds) {


        // 直接构造更新条件，避免先查询再更新
        LambdaUpdateWrapper<CircleTopicReply> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(CircleTopicReply::getReadFlag, 1)
                .eq(CircleTopicReply::getDelFlag, 0)
                .eq(CircleTopicReply::getReadFlag, 0)
                .ne(CircleTopicReply::getUid, uid)
                .in(CircleTopicReply::getTopicId, topicIds)
                .and(wrapper -> wrapper
                        .exists("SELECT 1 FROM circle_topic WHERE id = topic_id AND uid = {0}", uid)
                        .or()
                        .eq(CircleTopicReply::getReplyTo, uid));

        // 直接执行更新操作
        circleTopicReplyDao.update(null, updateWrapper);

    }

    public CircleTopicReply selectById(Long id) {
        return circleTopicReplyDao.getById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(CircleTopicReply reply) {
        circleTopicReplyDao.save(reply);
    }

    public Page<CircleTopicReply> page(Page<CircleTopicReply> topicReplyPage, LambdaQueryWrapper<CircleTopicReply> wrapper) {
        return circleTopicReplyDao.page(topicReplyPage, wrapper);
    }

    public List<CircleTopicReply> list(LambdaQueryWrapper<CircleTopicReply> wrapper) {
        return circleTopicReplyDao.list(wrapper);
    }
}